package com.sx.baseproject.framework.adapter.binding;

import android.view.View;

import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.example.mvvm.rx.RxViewUntil;
import com.google.android.flexbox.FlexboxLayout;
import com.sx.baseproject.BR;
import com.sx.baseproject.framework.adapter.binding.bean.CallBeanBack;
import com.sx.baseproject.framework.adapter.binding.bean.FlexEntity;

import java.util.List;

public class FlexBoxBinding {

    @BindingAdapter(value = {
            "flexData","flexEntities", "flexTextView", "flexMonitor"}, requireAll = false)
    public static void flexBoxBind(FlexboxLayout flexboxLayout, List<String> data, List<FlexEntity> entities, int id, CallBeanBack<String> callBeanBack) {
        if (flexboxLayout != null) {
            flexboxLayout.removeAllViews();
            if (data != null) {
                for (String datum : data) {
                    View view = View.inflate(flexboxLayout.getContext(), id, null);
                    ViewDataBinding bind = DataBindingUtil.bind(view);
                    bind.setVariable(BR.flag, datum);
                    if (callBeanBack != null) {
                        RxViewUntil.setClickShake(view, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                callBeanBack.getCallBack(datum);
                            }
                        });
                    }
                    flexboxLayout.addView(view);
                }
            } else if (entities != null) {
                flexboxLayout.removeAllViews();
                for (FlexEntity entity : entities) {
                    View view = View.inflate(flexboxLayout.getContext(), id, null);
                    ViewDataBinding bind = DataBindingUtil.bind(view);
                    bind.setVariable(BR.flag, entity);
                    if (entity.mIntent != null) {
                        RxViewUntil.setClickShake(view, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                flexboxLayout.getContext().startActivity(entity.mIntent);
                            }
                        });
                    }
                    flexboxLayout.addView(bind.getRoot());
                }

            }


        }
    }


}
